
#include "reservedWords.h"
#include <assert.h>
#include "Argument.h"

pString RWFirst(pAnonymousStatement statement)
{
	assert(statement != NULL);
	int debug = 0;
	if (debug) printf("In RWFirst\n");

	if (statement == NULL || statement->argumentList[0] == NULL)
		return String_new(NULL);

	if (debug) printf("Past RWFirst guard\n");
	pArgument arg = statement->argumentList[0];
	pString evaled = arg->eval(arg, statement);

	if (debug) printf("Evaled in RWFirst\n");
	if (evaled == NULL || evaled->cString == NULL)
		return String_new(NULL);
	if (debug) printf("Returning first char in %s for RWFirst.\n", evaled->cString);
	pString ret = String_substring(evaled, 0, 1);
	return ret;
}

pString RWRest(pAnonymousStatement statement)
{
	assert(statement != NULL);
	int debug = 0;
	if (debug) printf("In RWRest\n");

	if (statement == NULL || statement->argumentList[0] == NULL)
		return String_new(NULL);

	if (debug) printf("Past RWFirst guard\n");
	pArgument arg = statement->argumentList[0];
	pString evaled = arg->eval(arg, statement);

	if (debug) printf("Evaled in RWFirst\n");
	if (evaled == NULL || evaled->cString == NULL)
		return String_new(NULL);
	if (debug) printf("Returning first char in %s for RWFirst.\n", evaled->cString);
	pString ret = String_substring(evaled, 1, 0);
	return ret;
}

pString RWAppend(pAnonymousStatement statement)
{
	assert(statement != NULL);
	int debug = 0;
	if (debug) printf("In RWRest\n");

	if (statement == NULL || statement->argumentList[0] == NULL)
		return String_new(NULL);

	ArgumentList args = statement->argumentList;
	if (args == NULL) return String_new(NULL);
	pString ret = String_new(NULL);

	int i = 0;
	pArgument currentArg = args[i];
	while (currentArg != NULL)
	{
		pString toAppend = currentArg->eval(currentArg, statement);
		ret = ret->concat(ret, toAppend);
		i++;
		currentArg = args[i];
	}

	return ret;
}

pString RWIsEqual(pAnonymousStatement statement)
{
	assert(statement != NULL);
	int debug = 0;
	if (debug) printf("In RWRest\n");

	if (statement == NULL || statement->argumentList[0] == NULL)
		return String_new(NULL);

	// if less then 4 arguments, return empty string
	int argumentLength = ArgumentList_getLength(statement->argumentList);
	if (argumentLength < 4)
		return String_new(NULL);

	pString ret = String_new(NULL);
	ArgumentList args = statement->argumentList;
	pString first = args[0]->eval(args[0], statement);
	pString second = args[1]->eval(args[1], statement);
	if (String_equals(first, second))
		return args[2]->eval(args[2], statement);
	else
		return args[3]->eval(args[3], statement);
}

pString RWEcho(pAnonymousStatement statement)
{
	assert(statement != NULL);
	int debug = 0;
	if (debug) printf("In RWRest\n");

	if (statement == NULL || statement->argumentList[0] == NULL)
		return String_new(NULL);

	// only works for single statement
	int i = 0;
	pArgument current = statement->argumentList[i];
	pString ret = String_new(NULL);
	pString currentRet = current->eval(current, statement);
	while (current != NULL && currentRet != NULL && currentRet->cString != NULL)
	{
		printf("%s", currentRet->cString);
		ret = ret->concat(ret, currentRet);
		i++;
		current = statement->argumentList[i];
		if (current != NULL) currentRet = current->eval(current, statement);
	}

	printf("\n");
	return ret; // "as if append was called on the arguments"
}

pString RWExit()
{
	exit(0); // success
	return NULL;
}

pString RWInput()
{
	char* cStr = GC_malloc(200*sizeof(char));
	scanf("%s", cStr);
	return String_new(cStr);
}
